<template>
  <page-header-wrapper :title="false">
    <a-card>
      <span slot="title">
        <label class="back-title" @click="$router.push({ name: 'ha_task' })"><a-icon type="left" />执行日志</label>
      </span>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :xxl="6" :lg="8" :sm="8">
              <a-form-item label="执行IP">
                <a-input placeholder="请输入IP地址" v-model="queryParam.f_executeip" />
              </a-form-item>
            </a-col>
            <a-col :xxl="6" :lg="8" :sm="8">
              <a-form-item label="执行结果">
                <a-select v-model="queryParam.f_executeresult">
                  <a-select-option value="">
                    全部
                  </a-select-option>
                  <a-select-option value="1">
                    成功
                  </a-select-option>
                  <a-select-option value="0">
                    失败
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="searchHa_task_log" icon="search">查询</a-button>
                <a-button icon="reload" style="margin-left: 8px" @click="reQuery">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div style="margin-bottom: 16px">
          <a-button type="primary" style="margin-left: 8px" @click="exportExcel" icon="export">导出调度任务执行日志</a-button>
      </div>
      <a-table
      	:scroll="{ x: 1500, y: 470 }"
        :columns="ucolumns"
        :data-source="udata"
        :pagination="upagination"
        :loading="loading"
        rowKey="id"
        @change="tableChange"
        size="middle"
        :bordered="ubordered"
      >
      <span slot="serial" slot-scope="text, record, index">
        {{(upagination.current-1)*upagination.pageSize+parseInt(index)+1}}
      </span>
      <span slot="result" slot-scope="text, record">
        <template v-if="record.executeresult === 0" >失败</template>
        <template v-if="record.executeresult === 1" >成功</template>
      </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<style lang="less" src="@/style/base.less"></style>
<style>
@import '../../style/css/style.css';
</style>

<script>
import {delHa_task_logs, delHa_task_log, viewHa_task_log, saveHa_task_log, queryHa_task_logList, output } from '@/api/base/ha_task_log'
export default {
    data () {
        return {
            advanced: false, // 查询条件是否展开
            loading: false, // 遮罩是否显示
            visible: false, // 抽屉是否可见
            ubordered: false, // 表格边框是否显示
            isView: false, // 是否查看
            queryParam: {
              f_executeresult: ''
            }, // 查询条件
            selectedRowKeys: [], // 选中的行id
            udata: [], // 表格数据
            upagination: {// 表格分页参数
                defaultPageSize: 20,
                total: 0,
                current: 1,
                pageSize: 20,
                showTotal: total => `共${total}条数据`,
                showSizeChanger: true,
                pageSizeOptions: ['20', '50', '100']
            },
            ucolumns: [
            {
              title: '序号',
              width: '50px',
              align: 'center',
              fixed: 'left',
              scopedSlots: { customRender: 'serial' }
            },
            
            {
              title: '任务名称',
              dataIndex: 'name',
              width: '1',
              sorter: (a, b) => String(a.name).localeCompare(String(b.name)),
              align: 'center'
            },
            {
              title: '执行IP',
              dataIndex: 'executeip',
              width: '1',
              sorter: (a, b) => String(a.executeip).localeCompare(String(b.executeip)),
              align: 'center'
            },
            {
              title: '执行时长（秒）',
              dataIndex: 'executelong',
              width: '1',
              sorter: (a, b) => a.executelong - b.executelong,
              align: 'center'
            },
            {
              title: '执行结果',
              dataIndex: 'executeresult',
              width: '1',
              sorter: (a, b) => a.executeresult - b.executeresult,
              align: 'center',
              scopedSlots: { customRender: 'result' },
            },
            {
              title: '错误信息',
              dataIndex: 'executeerr',
              width: '1',
              sorter: (a, b) => String(a.executeerr).localeCompare(String(b.executeerr)),
              align: 'center'
            },
            {
              title: '执行时间',
              dataIndex: 'executetime',
              width: '1',
              sorter: (a, b) => String(a.executetime).localeCompare(String(b.executetime)),
              align: 'center'
            }
          ], // 表头
          taskid: ''
        }
    },
    created () {
      this.taskid = this.$route.params.taskid
      this.queryParam.f_taskid = this.taskid
    	this.findHa_task_logList(this.queryParam)
    },
    methods: {
    	/**
         *  查询枚举值,没有用到枚举作为查询条件的请删除
         */
    	findEnumByName () {
	      const parameter = [
	      ]
	      parameter.forEach(item =>{
	      	queryEnumerationValuesByName({'enName': item.enumName}).then(res => {
		        console.log('query' + item.enumName, res.data)
		        this[`${item.enname}List`] = res.data
		        this[`${item.enname}List`].unshift({ id: '', description: '全部' })
		        console.log(this[`${item.enname}List`])
	      	})
	      })
	    },
        /**
         *  查询调度任务执行日志列表
         */
        findHa_task_logList (queryParam) {
          const a = Date.now()
          console.log('queryParam', queryParam)
          queryHa_task_logList(queryParam).then(res => {
            this.loading = true
            console.log('queryHa_task_logList:', res.data)
            this.udata = res.data.records
            // 将当前页码和数量与后台绑定
            this.upagination.total = res.data.total
            this.upagination.current = res.data.current
            this.upagination.pageSize = res.data.size
            // 如果查询时间太短就给一个0.5秒的加载效果
            const b = Date.now()
            if ((b - a) < 500) {
              setTimeout(
                () => { this.loading = false },
                500
              )
            } else {
              this.loading = false
            }
          })
        },
        /**
         * 重置查询条件并查询数据
         */
        reQuery () {
          this.queryParam = {
          }
          this.queryParam.taskid = this.taskid
          this.findHa_task_logList(this.queryParam)
        },
        /**
         * 翻页、设置页数量时触发，将分页组建的当前页与大小传给后台
         */
        tableChange (pagination, filters, sorter, { currentDataSource }) {
            this.queryParam.pageNumber = pagination.current
            this.queryParam.pageSize = pagination.pageSize
            if (sorter != null && sorter.order != null) {
                this.queryParam.sort = sorter.field
                this.queryParam.order = sorter.order
            }
            this.findHa_task_logList(this.queryParam)
        },
        /**
         * 查询
         */
        searchHa_task_log () {
          // 查询时将当前页码重置为1，否则会出现有数据但不显示的问题
            this.queryParam.pageNumber = 1
            // this.upagination.current = 1
            this.findHa_task_logList(this.queryParam)
        },
        /**
         * 选中行数据时触发
         */
        onSelectChange (selectedRowKeys) {
            console.log('selectedRowKeys changed: ', selectedRowKeys)
            this.selectedRowKeys = selectedRowKeys
        },
        /**
         * 打开抽屉
         */
        showDrawer () {
          this.visible = true
        },
        /**
         * 收回抽屉
         */
        onClose () {
          this.visible = false
        },
        /**
         * 进入新增页面
         */
        handleAdd () {
          this.showDrawer()
          setTimeout(() => {
            this.$refs.view.resetHa_task_log()
          }, 10)
        },
        /**
         * 进入编辑页面
         */
        handleEdit (res, isView) {
          this.isView = isView
          console.log('view', res.id)
          this.showDrawer()
          setTimeout(() => {
            this.$refs.view.findHa_task_logById(res.id, isView)
          }, 10)
        },
        /**
         * 删除调度任务执行日志
         */
        delHa_task_logById (res) {
          delHa_task_log(res.id).then(res => {
              console.log('delHa_task_log:', res)
              if (res.code !== 0) {
                this.$message.success(res.msg)
              } else {
                this.selectedRowKeys = []
                this.searchHa_task_log()
                this.$message.success('删除成功')
              }
          })
        },
        /**
         * 批量删除调度任务执行日志
         */
        delHa_task_logs () {
          delHa_task_logs(this.selectedRowKeys.toString()).then(res => {
            console.log('delHa_task_logs:', res)
            if (res.code !== 0) {
                this.$message.success(res.msg)
              } else {
                this.selectedRowKeys = []
                this.searchHa_task_log()
                this.$message.success('删除成功')
              }
          })
        },
        /**
        *  导出Excel
        */
      	exportExcel () {
        output(this.queryParam).then(response => {
          var blob = new Blob([response])
          var downloadElement = document.createElement('a');
        　var href = window.URL.createObjectURL(blob); //创建下载的链接
        　downloadElement.href = href;
        　downloadElement.download = '调度任务执行日志.xls'; //下载后文件名
        　document.body.appendChild(downloadElement);
        　downloadElement.click(); //点击下载
        　document.body.removeChild(downloadElement); //下载完成移除元素
        　window.URL.revokeObjectURL(href); //释放掉blob对象 
        }).catch(function() {
          console.log(error)
        })
      }
    }
}
</script>